1. Fabric Lab
Design an Apache Camel Route using the Wiki editor and provision a local container
Develop a Camel project using JBoss Developer Studio and deploy it into the Fabric server using the Fabric8 Maven plug-in
Migrate the project to a new profile version and roll it back
Virtualize Camel endpoints and load balance requests
The lab exercises and solutions are available in the following zip archives:
To use the code in these exercises, you must connect to the OpenShift gear demo application to clone the Git exercises project locally, to compile it using Maven, and to deploy it into a JBoss Fuse server.
Next, you set up some JBoss Fuse containers and deploy the exercises.
1.1. Use SSH to Connect to Your Online Lab Account
Your lab instructions include the steps required to access your OpenShift gear via SSH here.
After you connect to your OpenShift gear, you can log on from JBoss Developer Studio or within your DOS/Unix terminal.
1.2. Clone the Project and Build the Code
After you are connected, use the following commands to set up a local Maven repository:
cd $OPENSHIFT_DATA_DIR echo -e "<settings>\n <localRepository>$OPENSHIFT_DATA_DIR/.m2/repository</localRepository>\n</settings>\n" > settings.xmlClone the
camel-labsproject:git clone https://github.com/gpe-mw-training/camel-labs.git
Change to the
camel-labsdirectory and checkout thesolutionbranch:cd camel-labs/ git checkout solutionUse the following command to build the code:
mvn clean install -DskipTests=true -s $OPENSHIFT_DATA_DIR/settings.xml
1.3. Deploy it
In the module 1 lab exercises, you created a demo JBoss Fuse server and you deployed a Camel Twitter` application to explore the Fuse technology.
If you have not deleted this OpenShift gear, you will reuse it here to deploy the compiled code to the local Maven Proxy.
This code is used by JBoss Fuse to provision and deploy the artifacts into the various JBoss Fuse containers.
The information that you need is the username, the password, and the name of your OpenShift gear machine (e.g: demo-fuse.apps.ose.opentlc.com).
You will use this information, and the Maven deploy command, to copy and paste your artifacts into the Maven Proxy directory of JBoss Fuse.
Execute the following commands, and replace the username, password, and gear_machine variables with the values that correspond to your setup:
mvn clean deploy -DaltDeploymentRepository=fabric::default::http://username:password@gear_machine/maven/upload -DskipTests=true -s $OPENSHIFT_DATA_DIR/settings.xml
Before deploying new Maven artifacts, (for example, after pushing new changes to the Git repository), remove what was previously deployed under this directory
and restart the demo application. |
cd $OPENSHIFT_DATA_DIR
rm rf -f ../../fuse/container/data/maven/proxy/tmp1.4. Design a Camel Route Using the Wiki Editor
In this exercise, you create a new Apache Camel route using the Wiki editor in the JBoss Fuse Management Console. Next, you include your new route in a profile, and then deploy it in a new container on your OpenShift machine.
To design your new Apache Camel route, you will use the Blueprint DSL language and edit a Camel OSGI Blueprint XML document.
From your
democontainer, use your web browser to open the JBoss Fuse Management Console.Click the Wiki tab and expand the tree to locate the
gpe/exercisefolder.Create a profile:
Click Fabric8 Profile, enter
cameldemoas the Name, and then click Create.On the screen that appears, select Fabric profile as the document type, and enter
cameldemoas the name of the document you want to create.Figure 1. Create Document
After the profile is created, use the Wiki editor to design an Apache Camel route:
Click Create again.
In the popup window, select Camel OSGI Blueprint XML document as the document type and camel-blueprint.xml as the file name.
The Wiki editor refreshes and a dummy route appears on the screen.
Figure 2. Wiki EditorSelect Log as the last endpoint of the route.
Click Add to add a new node.
In the Add Node window, under the Transformation category, select Set Header, and then click Add.
Figure 3. Add Node WindowThe route refreshes and a new endpoint displays at the end of the route.
Reselect the Set Header endpoint and define the following:
Expression:
Hello from GPE Team membersLanguage:
simpleHeader Name:
simpleFigure 4. Define Set Header
Click Apply, and then click Save to refresh the editor.
Reselect the last processor of the route and add a Log processor node:
Select the last endpoint, and click Add.
In the popup window, under the Endpoints category, select the Log processor.
Click Add.
Under Message, enter
Header Simple : ${header.simple}.Figure 5. Define LogClick Apply, and then click Save to finalize the route.
Figure 6. Complete RouteClick View Source to review the generated DSL.
Figure 7.camel-blueprint.xmlSource
Finalize the definition of the project/profile so you can deploy this route in a Fabric managed container:
Select the cameldemo.profile link from the Wiki tree.
Figure 8. Wiki TreeAfter the screen refreshes, click the small green editor icon under Parents.
Figure 9.cameldemoWikiUse Feature as the filter token for the profile selections.
Figure 10. Filter Profile SelectionsClick Change Parents and wait for the editor to refresh.
Figure 11. Edit ParentsAdd the Camel route by clicking the Bundles tab and adding the location of the
blueprint:profile:camel-blueprint.xmlfile.Figure 12. Add Bundle - Part 1Figure 13. Add Bundle - Part 2The Blueprintprotocol is used to create a bundle, which is a JAR file that contains your XML route definition. This file is located under theOSGI-INF/blueprint/camel-blueprint.xmldirectory . When you provision your Fabric container, this is the bundle you will deploy.
Deploy this profile/project into a new Fuse container:
Click New and enter
cameldemoas the Container Name.Enter your OpenShift platform credentials to register with the server, so you can create a new Fuse container in your OpenShift gear.
Figure 14. Create New ContainerClick Create And Start Container.
Wait until you are redirected to the Container view on the Runtime tab. This is where you can select the container and open it.
Click the name of the container to discover its information: associated profiles, status, settings, urls.
Figure 15. Select ContainerClick Open to access the JBoss Fuse Management Console for this container.
Figure 16. Container DetailsClick the Logs tab to review the Filter logs content.
Figure 17. LogsClick the
Cameltab to review the deployed route, endpoints, metrics, and so on.Figure 18. Camel Tab - Diagram ViewFigure 19. Camel Tab - Attributes View
1.5. Develop a Camel Project Using JBoss Developer Studio
In this exercise, you develop a Camel project using JBoss Developer Studio. This project uses the camel-weather component to collect weather information from different locations and then publishes the results in the log. You will use the Camel Blueprint DSL language to design the route.
Open JBoss Developer Studio and create a new workspace named
cameldemo.Create a new JBoss Fuse project:
Select File → New → Fuse Project from the menu.
Skip the first screen, New Fuse project / Select project location, and click Next.
Set the project archetype to
camel-archetype-blueprint.Set the Group Id to
com.redhat.gpe.Set the Artifact Id to
fabric-forecasting-application.Set the Version to
1.0.Click Finish.
Figure 20. New Fuse ProjectThe project is created and should now appear in Project Explorer.
In the
pom.xmlfile, add two new dependencies:Copy and paste the
camel-coredependencies twice.Replace the first instance of
camel-corewithcamel-groovy.Replace the second instance of
camel-corewithcamel-weather.Figure 21. Dependencies inpom.xml
Prepare to design the Camel route:
In Project Explorer, expand
src/main/resources/OSGI-INF/blueprint.Open the
blueprint.xmlfile. The default route displays.Click the Source tab and remove the
<bean/>androutecontent.Figure 22. Route With Sections Removed
Create your first route:
Click the Design tab.
Drag the Generic from the palette and drop it on the workspace.
Click the endpoint you just created.
Under Properties, set the Endpoint Uri to
timer:foo?period=10000.Set the event to fire every
10s.Select File → Save.
Figure 23. From Endpoint
Add a Log processor:
Drag the Log processor from the palette and drop it on the workspace.
Link the two endpoints using the arrow of the From endpoint.
Set the Message to
Collecting weather info.Select File → Save.
Figure 24. Log Processor
Define cities for weather data collection:
Drag the SetBody processor from the palette (under Transformation), and drop it on the workspace.
Set the Language to
simple.In Expression, enter
Paris,London,Brusselsas the cities.Link this processor to the Log endpoint.
Select File → Save.
Figure 25. SetBody Processor
Split the body content:
Drag the Split EIP pattern from the palette (under Routing), and drop it on the workspace.
Set the Language to
groovy.In Expression, enter
request.body.split(',')to split the list of cities with the,separator.Select File → Save.
Figure 26. Split Route
Set a header that uses the city name:
Drag SetHeader from the palette (under Transformation), and drop it on the workspace.
Set the Header Name to
CamelWeatherLocation.Set the Language to
simple.In Expression, enter
${body}.Select File → Save.
Figure 27. SetHeader
Add two more components:
Drag and drop a new Generic and set the Uri of the weather component to
weather:foo?period=1d&units=METRIC&mode=JSON.Figure 28. Set Endpoint UriAdd a Log endpoint to log the information received by the weather station.
Improve the log output by using a SetBody property with these values:
Language:
groovyExpression:
groovy.json.JsonOutput.prettyPrint(request.body)
Select File → Save.
Figure 29. Enhance Log Output
Before you deploy the project in the Fuse Fabric environment, test the project locally in JBoss Developer Studio:
In Project Explorer, expand
src/main/resources/OSGI-INF/blueprint.Right-click the
blueprint.xmlfile.Select Run as → Local Camel Context.
The Console tab appears and displays the execution log for the Camel Maven plug-in.
Figure 30. Console Tab With Execution Log
1.6. Deploy Your New Camel Project
In this exercise, you deploy the Camel project you just created into the Fabric server using the Fabric8 Maven plug-in.
First, you will make some modifications to deploy your project into a new profile and to document what you did for this application by adding a Readme.md file to the existing project. Next, you will include the Fabric Maven plug-in and define the properties for the profile, its parent, the features to deploy, and the Fabric server where it should be deployed.
Create a new folder under
src/main:Right-click the
mainfolder and select New → Folder.Name the folder
fabric8.Click Finish.
Create the
Readme.mdfile:Right-click the new
fabric8folder and select New → File.Name the file
Readme.md.Click Finish.
Open the
Readme.mdfile by selecting Open with → Text editor.Enter the following text to document your project:
# RedHat GPE - Weather Forecasting Application This is a _Weather Forecasting Application_ designed using the integration framework : Apache Camel with the component _camel-weather_ performing REST calls against the weather provider _http://openweathermap.org/_ to collect forecasting information about some cities.Save the file.
Edit the configuration file:
Open the
pom.xmlfile.In the
<plugins/>section, add the following Fabric8 Maven plug-in information after thecamel-maven-plugin:<plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>1.1.0.CR5</version> </plugin>Add the following
<configuration>section to specify the profile’s name, parent, and features to deploy:<configuration> <profile>gpe-exercise-forecastingapplication</profile> <parentProfiles>feature-camel</parentProfiles> <features>camel-groovy camel-weather</features> </configuration>Change the name of the project from
<name>A Camel Blueprint Route</name>to<name>GPE :: Exercise :: Forecasting Application</name>.Modify the OSGI metadata for the
maven-bundle-pluginby modifying the code below. This will accomplish the following:Import the packages needed to process the Groovy expression.
Rename the
Bundle-SymbolicNameto use yourcamelweather-demoproject.Use the
<DynamicImport-Package>option so that Groovy can generate new classes on the fly using thescript2233445566convention, and then allow the bundle’s classloader to import them.<instructions> <Bundle-SymbolicName>gpe-exercise-forecasting-application</Bundle-SymbolicName> <Import-Package>groovy.lang,*</Import-Package> <DynamicImport-Package>*</DynamicImport-Package> </instructions>
Provide the server’s URL to the Fabric8 Maven plug-in by adding a new property to the
<configuration/>tag forfabric8-maven-plugin. This tag must point to your OpenShift application. Here is an example, but you must replace the hostname shown here with your hostname:<configuration> ... <jolokiaUrl>http://demo-fuse.apps.ose.opentlc.com/jolokia</jolokiaUrl> </configuration>Save the file.
Validate the hostname URL by checking the response returned by the jolokia servlet:
Figure 31. Jolokia ServletDeploy the profile:
Declare the login/password used to access your Fuse Fabric server by entering a
<server>tag in the Maven settings configuration as described in setup the login / password.Make sure the Fuse Fabric server is started and the Fabric Ensemble server is running.
Open a DOS/Unix terminal within this project, and deploy the profile to the Fuse Fabric server with the
mvn fabric8:deploycommand.
Deploy the project:
Switch to the JBoss Fuse Management Console.
On the Wiki tab, select the Wiki that the profile created.
Figure 32. Wiki Created by ProfileOn the Runtime tab, deploy the project into a new
forecastingcontainer.Figure 33. Runtime Tab - Containers View
Verify deployment:
On the Camel tab, open the
forecastingcontainer and confirm that the Camel route is deployed and collecting weather forecasts.Figure 34. Camel Tab - Diagram ViewOn the Log tab, review the weather information collected for
Paris, London, ....Figure 35. Log Tab
1.7. Migrate and Rollback a Camel Project
In this exercise, you modify the project you created in JBoss Developer Studio and redeploy it to the same container, but to achieve this goal, you use the rolling upgrade and rollback feature of the Fuse Fabric technology. This means that you create a new 1.1 version of your profile and upgrade your container to that new version.
When you create a new profile version, a new Git branch is instantiated by the Fabric server, the profiles are cloned, and you can modify the content (features, bundles, properties, and so on). This new branch enables you to migrate or rollback to an existing application. In other words, if after making a set of changes you are not happy with the modifications, you can rollback to the previous profile version.
Edit the existing route:
Open JBoss Developer Studio and the workspace you created earlier in this lab.
In Project Explorer, open the
fabric-forecasting-applicationproject.Expand the
src/main/resources/OSGI-INF/directory and edit the Apache Camelblueprintroute.When the Fuse Integration editor appears, select the
setBodynode on the diagram.In the Expression field, add
New-York,Tokyo,Moscowto the list of cities.Save the route.
Edit the configuration file:
Edit the
pom.xmlfile to change the<version>number for this artifact to1.1.<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> ... <groupId>com.redhat.gpe</groupId> <artifactId>fabric-forecasting-application</artifactId> <packaging>bundle</packaging> <version>1.1</version>Because you want to migrate your project to a newer version of your Fuse Fabric profile, you also need to add a
<version>1.1</version>tag to the<configuration>section forfabric8-maven-pluginthat specifies the version you want to use:<plugin> <groupId>io.fabric8</groupId> <artifactId>fabric8-maven-plugin</artifactId> <version>1.1.0.CR5</version> <configuration> <profile>gpe-exercise-forecastingapplication</profile> <parentProfiles>feature-camel</parentProfiles> <features>camel-groovy camel-weather</features> <version>1.1</version> ...When you use the <version/>tag, the Fabric8 Maven plug-in calls the Fuse Fabric server and requests that a new version number be created (if the version number does not match the current version number). Behind the scenes, a new Git branch is created, and the existing profiles are cloned to that new branch—in this case, 1.1.Save the
pom.xmlfile.
Redeploy the profile:
Deploy the profile using the
mvn fabric8:deploycommand.Review the Maven log and observe that
version: 1.1was used to populate this new profile.[INFO] About to invoke mbean io.fabric8:type=ProjectDeployer on jolokia URL: http://localhost:8181/jolokia with user: admin [INFO] Result: DeployResults{profileUrl='null', profileId='gpe-exercise-forecastingapplication', versionId='1.1'} [INFO] Uploading file Readme.md to invoke mbean io.fabric8:type=Fabric on jolokia URL: http://localhost:8181/jolokia with user: admin [INFO] Performing profile refresh on mbean: io.fabric8:type=Fabric version: 1.1 profile: gpe-exercise-forecastingapplication
Migrate the profile in JBoss Developer Studio:
Open the JBoss Fuse Management Console for your Fuse Fabric server.
On the Runtime tab, select the Manage view.
On the left part of the panel, use the Filter field to select your
forecastprofile.Figure 36. Runtime Tab - Manage ViewUse the Versions dropdown to switch from profile
1.0to1.1.Figure 37. Version DropdownSelect
forecastingas the target container, and then click the green>button to apply the1.1profile to that container.Figure 38. Target ContainerWait a few moments for the container to migrate.
Figure 39. Migrated Container
Verify migration:
Open the
forecastingcontainer.Examine the log and confirm that the new cities you added, such as
Tokyo, appear in the forecast.Figure 40. Log With New Cities
Do one of the following to rollback the profile to version 1.0:
On the Runtime tab, select the Manage view, and repeat the same steps that you used to migrate the profile, but select version
1.0instead of version1.1.On the Runtime tab, select the Container view, click Migrate to, and then select version
1.0.Figure 41. Runtime Tab - Container View
1.8. Virtualize Camel Endpoints and Load Balance Requests
In this final exercise, you use the virtualization and load balancing capabilities of the Fuse Fabric technology to make a Camel endpoint transparent between the producer and the consumer.
In other words, the hostname and port number for the endpoint are registered into the Fabric repository (also known as the Apache Zookeeper server) under a logical group name. This group name
is also used by the client to query the registry for the hostname and port number of the server to be connected (HTTP Web container). If there are multiple HTTP
endpoints registered by Apache Camel in the Fuse Fabric registry, then the client is load balanced between the different servers.
Open the JBoss Fuse Management Console for your Fuse Fabric server.
On the Wiki tab, navigate to the
example/camelprofile.Select the
cluster.serverprofile.Review the definition of the Apache Camel route and observe the following:
There are different beans defined.
The bean definitions are important because they allow you to register your endpoint with Fabric.
The bean definitions also externalize the port number using the OSGI
propertyPlaceholdermechanism.
Review the following explanation of the different entries in the
blueprint.xmlfile.<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0" xsi:schemaLocation=" http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> <!-- osgi blueprint property placeholder --> <!-- 1. The property placeholder will help us to lookup about the properties : bind.address, port into the OSGI Config Admin service. These properties are externalized and not hardcoded within the route definition --> <cm:property-placeholder id="myConfig" persistent-id="io.fabric8.examples.camel.loadbalancing.server"/> <!-- 2. Lookup into the OSGI Service registry to retrieve the OSGI Curator Service which is a Zookeeper client calling the Zookeeper server running into the Fuse Fabric Server --> <reference id="curator" interface="org.apache.curator.framework.CuratorFramework"/> <!-- 3. Instantiate the Fabric Camel Component with the reference of the curator client --> <bean id="fabric-camel" class="io.fabric8.camel.FabricComponent"> <property name="curator" ref="curator"/> </bean> <!-- 4. Route exposing a Jetty Web Container and returning as response to the client a message with a header - karaf.name which is the karaf container name --> <camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/blueprint"> <!-- using Camel properties component and refer to the blueprint property placeholder by its id --> <propertyPlaceholder id="properties" location="blueprint:myConfig" prefixToken="[[" suffixToken="]]"/> <route id="fabric-server"> <from uri="fabric-camel:cluster:jetty:http://[[bind.address]]:[[port]]/fabric"/> // HERE IS WHERE THE JETTY WEB CONTAINER ENDPOINT WILL BE REGISTERED WITH THE GROUPNAME CLUSTER <log message="Request received : ${body}"/> <setHeader headerName="karaf.name"> <simple>${sys.karaf.name}</simple> </setHeader> <transform> <simple>Response from Fabric Container</simple> </transform> </route> </camelContext> <!-- 5. Lookup into the OSGI Service registry to retrieve the Apache Camel Component Resolver service used to do a lookup in order to load the JettyComponent from the classloader of the bundle exposing it --> <reference interface="org.apache.camel.spi.ComponentResolver" filter="(component=jetty)"/> </blueprint>Review the
cluster.clientprofile and the definition of the Apache Camel route:<!-- 1. Global error handler managing any kind of exceptions returned by a processor to the component issuing the creation of the exchange. The property redeliveryPolicyRef is defined with the next bean definition --> <errorHandler id="errorHandler" redeliveryPolicyRef="redeliveryPolicy"/> <!-- 2. This is the delivery policy that the error handler & camel will use to retry to call the processor where the exception occurs. After --> <redeliveryPolicyProfile id="redeliveryPolicy" maximumRedeliveries="3" redeliveryDelay="5000" retryAttemptedLogLevel="WARN"/> <!-- 3. The fabric camel component is used to do a lookup into the Zk registry to find the hostname/port number to be used to call the HTTP Server --> <route id="fabric-client" errorHandlerRef="errorHandler"> <from uri="timer://foo?fixedRate=true&period=1000"/> <setBody> <simple>Hello from Fabric Client to group "Cluster"</simple> </setBody> <to uri="fabric-camel:cluster"/> <log message=">>> ${body} : ${header.karaf.name}"/> </route>Deploy the
cluster.serverprofile into two containers managed by Fuse:Create two containers:
cluster1andcluster2.Assign the
cluster.serverprofile to the new containers.On the Runtime tab, select the Container view.
Confirm that the new containers were created and provisioned.
Figure 42. Runtime Tab With New Containers
Confirm that the endpoints of the routes are registered in the Fuse Fabric registry:
On the Runtime tab, select the Registry view.
Expand the
/fabric/registry/camel/endpoints/clustertree.Confirm that you have two endpoints registered with these addresses:
http://machine:8282/fabrichttp://machine:8383/fabricFigure 43. Registry Cluster ViewFigure 44. Registry - Machine 8382Figure 45. Registry - Machine 8382
With the servers running, use the
clientprofile to create the container managed by Fuse.Figure 46. Create Container From Client ProfileVerify load balancing:
Open the
clientcontainer.Click the Log tab.
Verify that the client received requests from the two servers.
The requests are now load balanced and are received randomly from one of the two servers.
Figure 47. Log for Load-Balanced Servers